import 'package:flutter/material.dart';

/// 一些通用的router动画
///
/// 缩放动画
class ScaleRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;

  ScaleRouter({required this.child, super.settings, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
            pageBuilder: (context, animation, secondaryAnimation) => child,
            transitionDuration: Duration(milliseconds: duration),
            transitionsBuilder: (context, anim, secondaryAnim, child) =>
                ScaleTransition(scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(parent: anim, curve: curve)), child: child));
}

/// 渐变透明动画
class FadeRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;

  FadeRouter({required this.child, super.settings, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
            pageBuilder: (context, animation, secondaryAnimation) => child,
            transitionDuration: Duration(milliseconds: duration),
            transitionsBuilder: (context, anim, secondaryAnim, child) =>
                FadeTransition(opacity: Tween(begin: 0.1, end: 1.0).animate(CurvedAnimation(parent: anim, curve: curve)), child: child));
}

/// 右--->左动画
class Right2LeftRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;

  Right2LeftRouter({required this.child, super.settings, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
            transitionDuration: Duration(milliseconds: duration),
            pageBuilder: (context, anim, secondaryAnim) => child,
            transitionsBuilder: (context, anim, secondaryAnim, child) => SlideTransition(
                position:
                    Tween<Offset>(begin: const Offset(1.0, 0.0), end: const Offset(0.0, 0.0)).animate(CurvedAnimation(parent: anim, curve: curve)),
                child: child));
}

/// 左--->右动画
class Left2RightRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;

  Left2RightRouter({required this.child, super.settings, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
            transitionDuration: Duration(milliseconds: duration),
            pageBuilder: (context, anim, secondaryAnim) => child,
            transitionsBuilder: (context, anim, secondaryAnim, child) => SlideTransition(
                position:
                    Tween<Offset>(begin: const Offset(-1.0, 0.0), end: const Offset(0.0, 0.0)).animate(CurvedAnimation(parent: anim, curve: curve)),
                child: child));
}

/// 上--->下动画
class Top2BottomRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;

  Top2BottomRouter({required this.child, super.settings, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
            transitionDuration: Duration(milliseconds: duration),
            pageBuilder: (context, anim, secondaryAnim) => child,
            transitionsBuilder: (context, anim, secondaryAnim, child) => SlideTransition(
                position:
                    Tween<Offset>(begin: const Offset(0.0, -1.0), end: const Offset(0.0, 0.0)).animate(CurvedAnimation(parent: anim, curve: curve)),
                child: child));
}

/// 下--->上动画
class Bottom2TopRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;

  Bottom2TopRouter({required this.child, super.settings, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
            transitionDuration: Duration(milliseconds: duration),
            pageBuilder: (context, anim, secondaryAnim) => child,
            transitionsBuilder: (context, anim, secondaryAnim, child) => SlideTransition(
                position:
                    Tween<Offset>(begin: const Offset(0.0, 1.0), end: const Offset(0.0, 0.0)).animate(CurvedAnimation(parent: anim, curve: curve)),
                child: child));
}

class AppDialogRoute<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  final double opacity;

  AppDialogRoute({required this.child, super.settings, this.duration = 500, this.opacity = 0.5, this.curve = Curves.fastOutSlowIn})
      : super(
            opaque: false,
            transitionDuration: Duration(milliseconds: duration),
            pageBuilder: (context, anim, secondaryAnim) => child,
            transitionsBuilder: (context, anim, secondaryAnim, child) =>
                FadeTransition(opacity: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(parent: anim, curve: curve)), child: child));

  @override
  Color get barrierColor => Colors.black.withOpacity(opacity);

  @override
  bool get barrierDismissible => true;

  @override
  String get barrierLabel => "";

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return FadeTransition(opacity: animation, child: child);
  }
}

/// 无动画路由
class NoAnimRouter<T> extends PageRouteBuilder<T> {
  final Widget child;

  NoAnimRouter({required this.child, super.settings})
      : super(
            opaque: false,
            pageBuilder: (context, animation, secondaryAnimation) => child,
            transitionDuration: const Duration(milliseconds: 0),
            transitionsBuilder: (context, animation, secondaryAnimation, child) => child);
}
